<template>
  <div class="detail-box">
    <i class="iconfont icon-jiantouarrowhead7 goback" @click="goBack"></i>
    <detail-header v-top :title="detail.name"></detail-header>
    <div
      class="poster"
      :style="{ 'background-image': 'url(' + detail.poster + ')' }"
    ></div>
    <div class="info-container">
      <div class="info-title">
        <h3>{{ detail.name }}</h3>
        <span class="film-type">{{ detail.item.name }}</span>
        <span class="grade">
          <em> {{ detail.grade }} </em>分</span
        >
      </div>
      <div class="info-box">
        <p>{{ detail.category }}</p>
        <p>{{ detail.premiereAt | timeFilter }}上映</p>
        <p>
          {{ detail.nation }}
          {{ detail.runtime ? " | " + detail.runtime + "分钟" : "" }}
        </p>
      </div>
      <div class="synopsis" :style="{ height: show ? 'auto' : '40px' }">
        {{ detail.synopsis }}
      </div>
      <p @click="show = !show" class="icon">
        <i
          class="iconfont"
          :class="show ? 'icon-jiantoushang' : 'icon-jiantouxia'"
        ></i>
      </p>
    </div>
    <div class="actors-box">
      <span class="actor-title">演职人员</span>
      <div>
        <detail-swiper :slideNum="4" swiperClass="actors">
          <div
            class="swiper-slide"
            v-for="item in detail.actors"
            :key="item.name"
          >
            <img :src="item.avatarAddress" alt="" />
            <p class="actor-name">{{ item.name }}</p>
            <p>{{ item.role }}</p>
          </div>
        </detail-swiper>
      </div>
    </div>

    <div class="actors-box">
      <span class="actor-title">剧照</span>
      <span class="all-photo">全部({{ detail.photos.length }})</span>
      <div>
        <detail-swiper :slideNum="2.2" swiperClass="photos" class="photos">
          <div
            class="swiper-slide"
            v-for="(item, index) in detail.photos"
            :key="item"
            @click="handlePreview(index)"
          >
            <div class="juzhao">
              <img :src="item" alt="" class="juphoto" />
            </div>
          </div>
        </detail-swiper>
      </div>
    </div>
    <div class="pickSitNow">选座购票</div>
  </div>
</template>

<script>
import Vue from "vue";
import http from "@/utils/http.js";
import moment from "moment";
import detailSwiper from "@/views/detail/DetailSwiper.vue";
import detailHeader from "@/views/detail/DetailHeader.vue";
import { mapState, mapMutations } from "vuex";
Vue.filter("timeFilter", (timestr) => {
  return moment(timestr * 1000).format("YYYY-MM-DD");
});
Vue.directive("top", {
  inserted(el) {
    console.log(el);
    el.style.opacity = 0;
    window.onscroll = () => {
      if (document.documentElement.scrollTop || document.body.scrollTop > 50) {
        el.style.opacity = 1;
      } else {
        el.style.opacity = 0;
      }
    };
  },
});
export default {
  data() {
    return {
      detail: {
        item: { name: "" },
        photos: { length: 0 },
      },
      show: false,
    };
  },
  computed: {
    ...mapState("tabBarModule", ["showTabBar"]),
  },
  methods: {
    ...mapMutations("tabBarModule", ["showTB", "hideTB"]),
    goBack() {
      this.$router.back();
    },
    handlePreview(index) {
      ImagePreview({
        images: this.detail.photos,
        startPosition: index, //指定从index位置开始显示
        closeable: true, //显示关闭按钮
        closeIconPosition: "top-right", //关闭按钮显示在顶部左边
        loop: false, //禁止循环切换
      });
    },
  },
  components: { detailSwiper, detailHeader },
  mounted() {
    http({
      url: `https://m.maizuo.com/gateway?filmId=${this.$route.params.id}&k=7533794`,
      headers: {
        "X-Host": "mall.film-ticket.film.info",
      },
    }).then((res) => {
      console.log(res);
      this.detail = res.data.data.film;
      console.log(this.detail.poster);
    });
    this.hideTB();
  },
  beforeDestroy() {
    console.log("this is show");
    this.showTB();
  },
};
</script>

<style lang="scss" scoped>
.detail-box {
  background-color: #f4f4f4;
  padding-bottom: 51px;
  .goback {
    position: fixed;
    left: 20px;
    top: 10px;
    /* margin-left: 20px; */
    background-color: rgba(255, 255, 255, 0.5);
    padding: 6px;
    border-radius: 50%;
    font-size: 18px;
    color: #000;
    z-index: 999;
  }
  .poster {
    width: 100%;
    height: 220px;
    background-position: center center;
    background-size: cover;
  }
  .info-container {
    padding: 10px;
    background-color: #fff;
    .info-title {
      text-align: left;
      h3 {
        display: inline;
        vertical-align: middle;
      }
      .film-type {
        color: #fff;
        background-color: #ccc;
        margin-left: 10px;
        padding: 1px 2px;
        font-size: 12px;
      }
      .grade {
        color: #ff5f16;
        margin-left: 10px;
        float: right;
        font-size: 12px;
        em {
          font-size: 18px;
        }
      }
    }
    .info-box {
      text-align: left;
      p {
        font-size: 14px;
        margin: 4px 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
  .synopsis {
    overflow: hidden;
  }
  .icon {
    text-align: center;
    padding: 8px 0;
  }
  .actors-box {
    margin-top: 10px;
    background-color: #fff;
    padding: 20px 10px 10px;
    .actor-title {
      font-size: 18px;
      color: #000;
    }
    .all-photo {
      float: right;
    }
    .swiper-slide {
      padding-top: 20px;
      text-align: center;
      img {
        width: 70px;
      }
      .juzhao {
        .juphoto {
          width: 150px;
        }
        width: 150px;
        height: 100px;
        background-color: #f4f4f4;
        overflow: hidden;
        display: flex;
        align-items: center;
      }

      p {
        font-size: 12px;
      }
      .actor-name {
        color: #000;
      }
    }
  }
  .pickSitNow {
    height: 50px;
    background-color: tomato;
    text-align: center;
    line-height: 50px;
    position: fixed;
    width: 100%;
    color: #fff;
    bottom: 0;
    left: 0;
  }
}
</style>